package com.rwhansen.humidor;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.io.File;
import java.awt.Toolkit;

public class Stats {
	
	public static Database cigarDatabase;
	public static Database humidorDatabase;
	public static Database ratingsDatabase;

	JFrame frmStats;

	public Stats(int tab) {
		frmStats = new JFrame();
		frmStats.setIconImage(Toolkit.getDefaultToolkit().getImage(Stats.class.getResource("/com/rwhansen/humidor/favicon.ico")));
		frmStats.setTitle("Stats");
		frmStats.setAlwaysOnTop(true);
		frmStats.setBounds(100, 100, 749, 505);
		frmStats.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		getDatabases();
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		frmStats.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		JPanel databasePanel = new JPanel();
		tabbedPane.addTab("Cigar Database", null, databasePanel, null);
		databasePanel.setLayout(null);
		
		JLabel lblNumberOfCigars = new JLabel("Number of Cigars in Database: " + cigarDatabase.getNumberOfCigars());
		lblNumberOfCigars.setBounds(10, 145, 628, 14);
		databasePanel.add(lblNumberOfCigars);
		
		JLabel lblAverageCaRating = new JLabel("Average CA Rating: " + cigarDatabase.getCAAverageRating());
		lblAverageCaRating.setBounds(10, 304, 628, 14);
		databasePanel.add(lblAverageCaRating);
		
		JPanel humidorPanel = new JPanel();
		tabbedPane.addTab("Humidor", null, humidorPanel, null);
		humidorPanel.setLayout(null);
		
		JLabel lblNumberOfCigars_1 = new JLabel("Number of Cigars in humidor: " + humidorDatabase.getNumberOfCigars());
		lblNumberOfCigars_1.setBounds(10, 103, 628, 14);
		humidorPanel.add(lblNumberOfCigars_1);
		
		JLabel lblNameOfHumidor = new JLabel("Name of Humidor: " + humidorDatabase.getName());
		lblNameOfHumidor.setBounds(10, 64, 628, 14);
		humidorPanel.add(lblNameOfHumidor);
		
		JLabel lblHumidorOwner = new JLabel("Humidor Owner: " + humidorDatabase.getOwner());
		lblHumidorOwner.setBounds(10, 25, 628, 14);
		humidorPanel.add(lblHumidorOwner);
		
		JLabel lblOldestCigarIn = new JLabel("Oldest Cigar in Humidor: " + humidorDatabase.getOldestCigar());
		lblOldestCigarIn.setBounds(10, 181, 628, 14);
		humidorPanel.add(lblOldestCigarIn);
		
		JLabel lblNewestCigarIn = new JLabel("Newest Cigar in Humidor: " + humidorDatabase.getNewestCigar());
		lblNewestCigarIn.setBounds(10, 220, 628, 14);
		humidorPanel.add(lblNewestCigarIn);
		
		JLabel lblValueOfCigars = new JLabel("Total Value of Cigars in Humidor: $" + humidorDatabase.getValueInDatabase());
		lblValueOfCigars.setBounds(10, 259, 628, 14);
		humidorPanel.add(lblValueOfCigars);
		
		JLabel lblMostExpensiveCigar = new JLabel("Most Expensive Cigar in Humidor: " + humidorDatabase.getMostExpensiveCigarString());
		lblMostExpensiveCigar.setBounds(10, 337, 628, 16);
		humidorPanel.add(lblMostExpensiveCigar);
		
		JLabel lblLeastExpensiveCigar = new JLabel("Least Expensive Cigar in Humidor: " + humidorDatabase.getLeastExpensiveCigarString());
		lblLeastExpensiveCigar.setBounds(10, 378, 628, 16);
		humidorPanel.add(lblLeastExpensiveCigar);
		
		JLabel lblMostOfOne = new JLabel("Most of one cigar: " + humidorDatabase.getCigarHighestQuantity());
		lblMostOfOne.setBounds(10, 419, 628, 16);
		humidorPanel.add(lblMostOfOne);
		
		JLabel lblAverageValueOf_1 = new JLabel("Average Value of the Cigars in Humidor: $" + humidorDatabase.getAverageCigarValue());
		lblAverageValueOf_1.setBounds(10, 298, 628, 14);
		humidorPanel.add(lblAverageValueOf_1);
		
		JLabel lblAverageCaRating_1 = new JLabel("Average CA Rating: " + humidorDatabase.getCAAverageRating());
		lblAverageCaRating_1.setBounds(10, 142, 628, 14);
		humidorPanel.add(lblAverageCaRating_1);
		
		JPanel ratingsPanel = new JPanel();
		tabbedPane.addTab("Ratings", null, ratingsPanel, null);
		ratingsPanel.setLayout(null);
		
		JLabel lblNumberOfCigars_2 = new JLabel("Number of cigars rated: " + ratingsDatabase.getNumberOfCigars());
		lblNumberOfCigars_2.setBounds(10, 17, 628, 14);
		ratingsPanel.add(lblNumberOfCigars_2);
		
		JLabel lblFirstCigarRated = new JLabel("First Cigar Rated: " + ratingsDatabase.getFirstCigarRated());
		lblFirstCigarRated.setBounds(10, 48, 628, 14);
		ratingsPanel.add(lblFirstCigarRated);
		
		JLabel lblMostRecentCigar = new JLabel("Most Recent Cigar Rated: " + ratingsDatabase.getMostRecentCigarSmoked());
		lblMostRecentCigar.setBounds(10, 79, 628, 14);
		ratingsPanel.add(lblMostRecentCigar);
		
		JLabel lblAverageCigarRating = new JLabel("Average Cigar Rating: " + ratingsDatabase.getMyAverageRating());
		lblAverageCigarRating.setBounds(10, 141, 628, 14);
		ratingsPanel.add(lblAverageCigarRating);
		
		JLabel lblAverageValueOf = new JLabel("Average Value of Cigar Rated: $" + ratingsDatabase.getAverageCigarValue());
		lblAverageValueOf.setBounds(10, 364, 628, 14);
		ratingsPanel.add(lblAverageValueOf);
		
		JLabel lblCigarsSmokedLastThirtyDays = new JLabel("Cigars Rated In The Last 30 Days: " + ratingsDatabase.getCigarsSmokedLastThirtyDays());
		lblCigarsSmokedLastThirtyDays.setBounds(10, 172, 628, 14);
		ratingsPanel.add(lblCigarsSmokedLastThirtyDays);
		
		JLabel lblCigarsSmokedThisMonth = new JLabel("Cigars Rated This Month: " + ratingsDatabase.getCigarsSmokedThisMonth());
		lblCigarsSmokedThisMonth.setBounds(10, 203, 628, 14);
		ratingsPanel.add(lblCigarsSmokedThisMonth);

		JLabel lblCigarsSmokedThisYear = new JLabel("Cigars Rated This Year: " + ratingsDatabase.getCigarsSmokedthisYear());
		lblCigarsSmokedThisYear.setBounds(10, 234, 628, 16);
		ratingsPanel.add(lblCigarsSmokedThisYear);
		
		JLabel lblCigarsSmokeLastMonth = new JLabel("Cigars Rated Last Month: " + ratingsDatabase.getCigarsSmokedLastMonth());
		lblCigarsSmokeLastMonth.setBounds(10, 267, 628, 14);
		ratingsPanel.add(lblCigarsSmokeLastMonth);

		JLabel lblCigarsSmokedLast = new JLabel("Cigars Rated Last Year: " + ratingsDatabase.getCigarsSmokedLastYear());
		lblCigarsSmokedLast.setBounds(10, 298, 628, 16);
		ratingsPanel.add(lblCigarsSmokedLast);
		
		JLabel lblMostExpensive = new JLabel("Most Expensive Cigar Rated: " + ratingsDatabase.getMostExpensiveCigarString());
		lblMostExpensive.setBounds(10, 331, 628, 16);
		ratingsPanel.add(lblMostExpensive);
		
		JLabel lblLeastExpensiveCigar_1 = new JLabel("Least Expensive Cigar Rated: " + ratingsDatabase.getLeastExpensiveCigarString());
		lblLeastExpensiveCigar_1.setBounds(10, 395, 628, 16);
		ratingsPanel.add(lblLeastExpensiveCigar_1);
		
		JLabel lblNumberOfPerfect = new JLabel("Number of Perfect Cigars Rated: " + ratingsDatabase.getNumberPerfectCigars());
		lblNumberOfPerfect.setBounds(10, 110, 628, 14);
		ratingsPanel.add(lblNumberOfPerfect);
		
		JLabel lblCostOfCigars = new JLabel("Cost of Cigars Smoked: $" + ratingsDatabase.getValueInDatabase());
		lblCostOfCigars.setBounds(10, 428, 628, 16);
		ratingsPanel.add(lblCostOfCigars);
		
		
		
		tabbedPane.setSelectedIndex(tab);
	}
	
	public static void getDatabases() {
		// Create-Get Cigar Database		
				String cigarDatabaseFileName = Database.CIGAR_DB_FILENAME;
				cigarDatabase = new Database(cigarDatabaseFileName);
				File file1 = new File(cigarDatabaseFileName);
				boolean exists1 = file1.exists();
				if (exists1) {
					cigarDatabase = Database.readDatabase(cigarDatabaseFileName);
				}
				
		//Create-Get Humidor Database		
				String humidorDatabaseFileName = Database.HUMIDOR_DB_FILENAME;
				humidorDatabase = new Database(humidorDatabaseFileName);
				File file2 = new File(humidorDatabaseFileName);
				boolean exists2 = file2.exists();
				if (exists2) {
					humidorDatabase = Database.readDatabase(humidorDatabaseFileName);
				}
				
		//Create-Get Rating Database		
				String ratingsDatabaseFileName = Database.RATINGS_DB_FILENAME;
				ratingsDatabase = new Database(ratingsDatabaseFileName);
				File file3 = new File(ratingsDatabaseFileName);
				boolean exists3 = file3.exists();
				if (exists3) {
					ratingsDatabase = Database.readDatabase(ratingsDatabaseFileName);
				}			
	}
}
